package com.ysk.testapplication.qcm.img_load;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.widget.ImageView;

import com.ysk.testapplication.LOG;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

/**
 * 图片加载 先用工具类形式写 之后再决定具体用哪种方案
 * 使用代理模式，可以使用 {@link #init(ImageLoadProxyImpl)}切换其他加载方案
 * 默认使用 Glide
 */
public class ImageLoadUtil {

  public @interface ImageType {
    int HEAD = 0x001;//头像
    int BORDER = 0x002;//边框
  }

  private final static int DEFAULT_WIDTH = 320;
  private final static int DEFAULT_HEIGHT = 320;

  private static ImageLoadProxyImpl mImageLoadProxyImpl = new DefaultProxy();

  public static void init(ImageLoadProxyImpl imp) {
    mImageLoadProxyImpl = imp;
  }

  public static void display(Context context, String path, ImageView view) {
    display(context, path, 0, view, DEFAULT_WIDTH, DEFAULT_HEIGHT, 0);
  }

  public static void display(Context context, String path, ImageView view, int errRes) {
    display(context, path, 0, view, DEFAULT_WIDTH, DEFAULT_HEIGHT, errRes);
  }

  public static void displayHead(final Context context, String path, ImageView view, int errRes) {
    mImageLoadProxyImpl.display(context, path, view, 0, errRes, 0, 0, ImageType.HEAD);
  }

  public static void displayBorder(final Context context, String path, ImageView view, int errRes) {
    mImageLoadProxyImpl.display(context, path, view, 0, errRes, 0, 0, ImageType.BORDER);
  }

  public static void display(Context context, String path, int defautShowRes, ImageView view,
                             int width, int height, int errRes) {
    mImageLoadProxyImpl.display(context, path, view, defautShowRes, errRes, width, height, 0);
  }

  public static void display(Context context, String path, int defautShowRes, ImageView view,
                             int width, int height, int errRes,
                             @ImageType int moreTag) {
    mImageLoadProxyImpl.display(context, path, view, defautShowRes, errRes, width, height, moreTag);
  }

  public static StringBuffer getRequestData(Map<String, String> params, String encode) {
    if (params == null || params.size() == 0) return new StringBuffer();
    //存储封装好的请求体信息
    StringBuffer stringBuffer = new StringBuffer();
    try {
      for (Map.Entry<String, String> entry : params.entrySet()) {
        if (entry.getValue() == null) continue;
        stringBuffer.append(entry.getKey()).append("=")
                    .append(URLEncoder.encode(entry.getValue(), encode)).append("&");
      }
      //删除最后的一个"&"
      //            stringBuffer.deleteCharAt(stringBuffer.length() - 1);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return stringBuffer;
  }
}
